<template>
  <div class="container">
    <div class="top">北京动态</div>
    <div class="box">
      <div class="top1">
        <div class="left">
          <span>标题：</span>
          <el-input v-model="input1" class="first"></el-input>
          <span>分类：</span>
          <el-select v-model="value" placeholder="请选择" class="second" @change="change">
            <el-option label="全部" value></el-option>
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.itemName"
              :value="item.itemValue"
            ></el-option>
          </el-select>
          <span>详细分类：</span>
          <el-select v-model="value4" placeholder="请选择" class="second">
            <el-option label="全部" value></el-option>
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.itemName"
              :value="item.itemValue"
            ></el-option>
          </el-select>
          <span>创建/更新日期：</span>
          <el-date-picker
            class="data"
            value-format="yyyy-MM-dd"
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
          <el-button type="primary" class="search" @click="search" v-if="_SHOWBTN('wzgl-xsxc-bjdt-ss')">查询</el-button>
          <el-button class="reset" @click="reset">重置</el-button>
        </div>
        <div class="right">
          <el-button type="primary" @click="add" class="add" v-if="_SHOWBTN('wzgl-xsxc-bjdt-xj')">+新建</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <el-table
        :data="tableList"
        style="width:1540px"
        border
        stripe
        :cell-style="changeCellStyle"
        :header-cell-style="rowClass"
      >
        <el-table-column type="index" label="序号" width="52px" align="center"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="sortNme1" label="分类" width="177px" align="center"></el-table-column>
        <el-table-column prop="sortNme2" label="详细分类" width="177px" align="center"></el-table-column>
        <el-table-column prop="publishTime" label="发布日期" align="center" width="240px"></el-table-column>
        <el-table-column prop="opTime" label="创建/更新日期" align="center" width="240px"></el-table-column>
        <el-table-column prop="status" label="是否上架" align="center" width="140px">
          <template slot-scope="scope">
            <el-switch
            
              v-model="scope.row.isTrue"
              active-color="#1677FF"
              inactive-color="#E8E8E8"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="223px">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="edit(scope.row.id)" class="five" v-if="_SHOWBTN('wzgl-xsxc-bjdt-xg')">编辑</el-button>
            <SingleDeletion :clearOne="clearOne" :id="scope.row.id" showBetCode="wzgl-xsxc-bjdt-sh"></SingleDeletion>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="fenye">
        <Pagination :total="total" :params=" params" :get="get"></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import SingleDeletion from "@/components/Popup/SingleDeletion.vue";
import Pagination from "@/components/Popup/Pagination.vue";
export default {
  components: {
    SingleDeletion,
    Pagination,
  },
  data() {
    return {
      params: {
        endTime: "",
        pageNum: this.$store.state.PortalManagement.pageNum,
        pageSize: this.$store.state.PortalManagement.pageSize,
        sortCode1: "",
        sortCode2: "",
        startTime: "",
        title: "",
       
      },
      id: 0,
      // 表格列表
      tableList: [],
      // 时间选择
      value1: "",
      // 总条数
      // 总条数
      total: 0,
      page: 1,
      size: 10,
      // 详细分类
      value4: "",
      input1: "",
      input2: "",
      // 分类
      value: "",
      value2: "",
      value3: true,
      data1: [],
      data2: [],
      data3: [],
      data4: [],
      // 分类
      options: [],
      // 详细分类
      options1: [],
    };
  },
  created() {
    // 分类
    this.Api.Fenlei({
      typeCode: "BJ_INFO_SORT1",
    }).then((res) => {
      this.options = res.data;
      console.log(this.options);
    });
    this.Api.Fenlei1({
      typeCode: "BJ_INFO_SORT2",
    }).then((res) => {
      console.log(res);
      this.data1 = res.data;
    });
    this.get();
  },
  methods: {
    // 分类下拉框事件

    change(e) {
      this.options1 = [];
      this.options.forEach((res) => {
        if (e == 1 && res.itemValue == 1) {
          this.options1 = this.data1;
        } else if (e == 2 && res.itemValue == 2) {
          this.options1 = [];
          this.options1 = this.data2;
        } else if (e == 3 && res.itemValue == 3) {
          this.options1 = [];
          this.options1 = this.data3;
        } else if (e == 4 && res.itemValue == 4) {
          this.options1 = [];
          this.options1 = this.data4;
        }
      });
    },
    //获取分类列表
    get() {
      this.Api.getBeijing(this.params).then((res) => {
        if (res.code == 0) {
          this.tableList = [];
          this.tableList = res.data.records;
          this.tableList.map((item) => {
            if (item.publishStatus == 5) {
              this.$set(item, "isTrue", true);
            } else if (item.publishStatus == 1) {
              this.$set(item, "isTrue", false);
            }
            this.options.map((item1) => {
              if (item1.itemValue == item.sortCode1) {
                this.$set(item, "sortNme1", item1.itemName);
              }
            });
            this.data1.map((item1) => {
              if (item1.itemValue == item.sortCode2) {
                this.$set(item, "sortNme2", item1.itemName);
              }
            });
          });

          this.total = res.data.total;
        }
      });
    },
    // // 分页
    // //数据条数发生变化
    // handleSizeChange(newSize) {
    //   this.size = newSize;
    //    if (this.value == "" && this.input1 == ""&& this.value4 == ""&& this.value1 == "") {
    //     this.get();
    //   } else {
    //     this.search();
    //   }
    // },
    // //翻页触发
    // handleCurrentChange(currPage) {
    //   this.page = currPage;
    //   if (this.value == "" && this.input1 == ""&& this.value4 == ""&& this.value1 == "") {
    //     this.get();
    //   } else {
    //     this.search();
    //   }
    // },
    changeCellStyle(row, column, rowIndex, columnIndex) {
      if (row.column.label === "标题") {
        return "padding-left: 35px"; // 修改的样式
      } else {
        return "";
      }
    },
    // 是否上架状态改变
    userStateChange(row) {
      // 上架
      if (row.isTrue == true) {
        this.Api.BjUp({ id: row.id }).then((res) => {
          if (res.code == 0) {
            this.get()
            return this.$message.success("上架成功");
          } else {
            row.isTrue = !row.isTrue;
            return this.$message.error("上架失败");
          }
        });
      }
      // 下架
      if (row.isTrue == false) {
        this.Api.BjDown({ id: row.id }).then((res) => {
          if (res.code == 0) {
            this.get()
            return this.$message.success("下架成功");
          } else {
            row.isTrue = !row.isTrue;
            return this.$message.error("下架失败");
          }
        });
      }
    },
    // 搜索
    search() {
      var dateTime = this.value1;
      // 开始时间
      var startDate = "";
      // 结束时间
      var endDate = "";
      if (dateTime != null && dateTime != "" && dateTime != undefined) {
        startDate = this.value1[0];
        endDate = this.value1[1];
      }
      this.Api.getBeijing({
        endTime: endDate,
        pageNum: this.page,
        startTime: startDate,
        sortCode1: this.value,
        sortCode2: this.value4,
        title: this.input1,
        pageSize: this.size,
      }).then((res) => {
        this.total = res.data.total;
        this.tableList = res.data.records;
        this.tableList.map((item) => {
          if (item.publishStatus == 5) {
            this.$set(item, "isTrue", true);
          } else if (item.publishStatus == 1) {
            this.$set(item, "isTrue", false);
          }
        });
        this.Api.getBeijing({
          endTime: "",
          pageNum: this.page,
          sortCode1: "",
          sortCode2: "",
          startTime: "",
          title: "",
          pageSize: this.size,
        }).then((res) => {
          if (res.code == 0) {
            this.tableList.map((item) => {
              if (item.publishStatus == 5) {
                this.$set(item, "isTrue", true);
              } else if (item.publishStatus == 1) {
                this.$set(item, "isTrue", false);
              }
              this.options.map((item1) => {
                if (item1.itemValue == item.sortCode1) {
                  this.$set(item, "sortNme1", item1.itemName);
                }
              });
              this.data1.map((item1) => {
                if (item1.itemValue == item.sortCode2) {
                  this.$set(item, "sortNme2", item1.itemName);
                }
              });
            });
          }
        });
      });
    },
    // 重置
    reset() {
      (this.value1 = ""),
        (this.input1 = ""),
        (this.value = ""),
        (this.value4 = ""),
        (this.params = {}),
        (this.params = {
          endTime: "",
          pageNum: 1,
          sortCode1: "",
          sortCode2: "",
          startTime: "",
          title: "",
          pageSize: 10,
        });
      this.get();
    },
    // 删除
    clearOne(id) {
      let ID = { id: id };
      this.Api.BjDelete(ID).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.get();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    rowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0 && columnIndex === 1) {
        //指定坐标
        return "padding-left:35px";
      } else {
        return "";
      }
    },
    // 新建
    add() {
      this.$router.push({ name: "BJXJ" });
    },
    // 编辑
    edit(id) {
      this.$router.push({ name: "BJBJ", query: { id: id } });
    },
  },
};
</script>
<style lang="less" scoped>
.top {
  width: 1540px;
  height: 68px;
  line-height: 68px;
  color: #333333;
  font-size: 20px;
  border-bottom: 1px solid #dddddd;
  box-sizing: border-box;
}

.top1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.left {
  display: flex;
  margin-top: 19px;
  align-items: center;
}
.right {
  margin-top: 19px;
}
/deep/.left .el-input__inner {
  width: 154px;
  height: 36px;
}
/deep/.left .el-input {
  width: 154px;
}
.left span {
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #333333;
}
.el-button {
  width: 52px;
  height: 36px;
  padding: 0;
}
.five {
  width: 60px;
  height: 28px;
  margin-right: 21px;
}
.first,
.second,
.third {
  margin-right: 20px;
}
/deep/ .add.el-button {
  width: 80px;
}
/deep/.left .data.el-input__inner {
  width: 350px;
  height: 36px;
}
/deep/.search.el-button {
  margin-left: 20px !important;
}
/deep/.reset.el-button {
  background: #d7dbe0;
}
/deep/.el-pagination {
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
  margin-left: 850px;
  margin-top: 46px;
}
/deep/.el-pagination {
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
  margin-left: 850px;
  margin-top: 46px;
}
/deep/.el-pagination__jump {
  margin-left: 0;
  font-weight: 400;
  color: #606266;
}
/deep/tbody {
  tr {
    height: 45px !important;
    td {
      padding: 0px 0 !important;
    }
  }
  tr td:last-child {
    .cell {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
/deep/tbody {
  tr td:nth-child(2) {
    .cell {
      padding-left: 35px;
    }
  }
}
.fenye {
  display: flex;
  justify-content: flex-end;
}
</style>